import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {InfoProvider} from "../../providers/InfoProvider";
import {HttpService} from "../../providers/HttpService";

/**
 * Generated class for the NewQuestionPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-new-question',
  templateUrl: 'new-question.html',
})
export class NewQuestionPage {

  user_info: any;

  question: any = {
    type: 1,
    content: null,
    user_id: null,
    user_code: null,
    user_phone: null,
    user_name: null,
    user_gender: null
  };

  constructor(public navCtrl: NavController, public navParams: NavParams,
              private infoProvider: InfoProvider,
              private httpService: HttpService) {
    this.user_info = this.infoProvider.getUserInfo();
    this.question.user_id = this.user_info.id;
    this.question.user_code = this.user_info.code;
    this.question.user_phone = this.user_info.phone;
    this.question.user_name = this.user_info.name;
    this.question.user_gender = this.user_info.gender;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad NewQuestionPage');
  }

  onQuestionSubmit() {
    console.log('onQuestionSubmit: ', this.question);
    if (!this.question.content) {
      console.log('content missing');
      return;
    }

    this.httpService.instance()
        .setUrl('/question/addQuestion')
        .setBody(this.question)
        .postRequest()
        .then(res => {
          if (res.status == 0) {
            this.navCtrl.pop();
          }
        });
  }
}
